const BehaviorAll = require("../../../behaviors/behavior_all.js");
const ServiceUser = require("../../../service/service_user.js");
const ServiceOrderManage = require("../../../service/service_orderManage.js");

Component({
  /**
   * behaviors
   */
  behaviors: [BehaviorAll],
  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 订单状态(默认:待发货)
     * 
     * 待付款=1
     * 待审核=2
     * 待发货=3
     * 已发货=4
     * 已完成=5
     */
    orderState: null,
    /**
     * 订单数据格式：
     */
    orders: [],
    /**
     * 开始页
     */
    startPage: 0
  },

  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const _this = this;
      ServiceUser.getInfo(function (data) {
        // 判断员工类型
        let state = null;
        if (data.branchCompanyDirectorFlag) {
          state = 3;
        } else {
          state = 4;
        }
        _this.setData({
          userInfo: data,
          orderState: state
        })
        // 获取订单
        ServiceOrderManage.page(0, state, function (data) {
          _this.setData({ orders: data.records })
        });
      });
    },

    /**
     * 触底事件
     */
    onReachBottom: function () {
      const _this = this;
      const filters = _this.data.filters;
      const orderState = _this.data.orderState;
      let startPage = this.data.startPage + 1;
      ServiceOrderManage.page(startPage, orderState, function (data) {
        _this.data.orders.push(...data.records);
        _this.setData({
          orders: _this.data.orders,
          startPage: startPage
        });
      });
    },

    /**
     * 选择Tab
     */
    onTab: function (e) {
      const _this = this;
      // 处理状态参数
      let state = e.currentTarget.dataset.state;
      if (state) {
        state = Number(state);
      } else {
        state = '';
      }
      _this.setData({
        orderState: state,
        startPage: 0
      });
      // 申请列表
      ServiceOrderManage.page(0, state, function (data) {
        _this.setData({ orders: data.records })
      });
    }
  }

})